package com.coolled.ei.utils.entityCreator;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;


public class DatabaseConfig {

    public List<Map<String, Object>> getTableColumns(String tableSchema, String tableName) {
        String sql = "SELECT COLUMN_NAME, DATA_TYPE " +
                "FROM INFORMATION_SCHEMA.COLUMNS " +
                "WHERE table_schema='"+tableSchema+"' and TABLE_NAME = '"+tableName+"'";
        return new JdbcTemplate(dataSource()).queryForList(sql);
    }
    public DataSource dataSource() {

        // 提取datasource下的属性
        String url = "jdbc:sqlserver://192.168.0.39:1433;databaseName=AKS2019;autoReconnectForPools=true;encrypt=true;trustServerCertificate=true;";
        String username ="sa";
        String password = "abcd1234!";
        String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

        return DataSourceBuilder.create()
                .url(url)
                .username(username)
                .password(password)
                .driverClassName(driverClassName)
                .build();
    }


}